package ar.edu.itba.pod.legajo45002;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import ar.edu.itba.pod.legajo45002.api.Result;
import ar.edu.itba.pod.legajo45002.api.SPNode;
import ar.edu.itba.pod.legajo45002.api.Signal;
import ar.edu.itba.pod.legajo45002.api.SignalProcessor;
import ar.edu.itba.pod.legajo45002.source.RandomSource;

/**
 * Hello world!
 *
 */
public class Client {
	private  String hostname;
	private final int port;
	
	RandomSource src = new RandomSource(1);

	public Client(String hostname, int port) {
		super();
		this.hostname = hostname;
		this.port = port;
	}
	public void start() {
		this.waitForCommand();
	}

	public void waitForCommand() {

		try {
			Registry registry = LocateRegistry.getRegistry(hostname, port);
			SignalProcessor	sp = (SignalProcessor) registry.lookup("SignalProcessor");
			SPNode node = (SPNode) registry.lookup("SPNode");
			
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			printOptions();
			while (true) {
				String line = in.readLine().toLowerCase();
				long start = System.nanoTime();
				if (line.equals("1")) {
					node.join("dualcluster");
				}
				else if (line.equals("2")) {
					node.exit();
				} else if (line.equals("3")) {
					node.getStats().print(System.out);
				} else if (line.equals("4")) {
				//	this.generate(sp, 100000);
				//	for (int i = 1; i < 10; i++ ){
					Signal s = src.next();
					System.out.println("Signal to compare: " +s);
					Result results = sp.findSimilarTo(s);
					System.out.println(">>> Result: " + results);
			//		}
				} else if (line.equals("5")) {
					this.generate(sp, 1);
				} else if (line.equals("6")) {
					this.generate(sp, 10);
				} else if (line.equals("7")) {
					this.generate(sp, 100);
				} else if (line.equals("8")) {
					this.generate(sp, 1000);
				} else if (line.equals("9")) {
					this.generate(sp, 50000);
				} else if (line.equals("0")) {
					return;
				} 
				else {
					System.out.println("Invalid option");
					printOptions();
					continue;
				}
				long end = System.nanoTime();
				System.out.println(">>> elapsed time: " + ((end - start) / 1000000000.0) + " seconds");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void generate(SignalProcessor sp, int amount) throws RemoteException {
		for (int i = 0; i < amount; i++) {
			Signal s =src.next(); 
		//	System.out.println(s);
			sp.add(s);

		}
	}

	private void printOptions() {
		System.out.println("Options:");
		System.out.println("1 - Join to cluster \"podcluster\"");
		System.out.println("2 - Exit cluster");
		System.out.println("3 - Print stats");
		System.out.println("4 - Process signal for matches");
		System.out.println("5 - Add 1 signal");
		System.out.println("6 - Add 10 signals");
		System.out.println("7 - Add 100 signals");
		System.out.println("8 - Add 1.000 signals");
		System.out.println("9 - Add 50.000 signals");
		System.out.println("0 - End");
		System.out.print("> ");
		System.out.flush();
	}

	/**
	 * Simple cluster client.
	 * @param args Command line arguments: <hostname> <port> of the node's RMI Registry
	 */
	public static void main(String[] args) throws RemoteException, NotBoundException {
		if (args.length < 2) {
			System.out.println("Command line parameters: SampleClient <host> <port> ");
			return;
		}
		Client client = new Client(args[0], Integer.valueOf(args[1]));
		client.start();
	}
}